<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14 0014
 * Time: 14:56
 */

namespace app\admin\controller;
use think\Controller;
use app\admin\model\Teacher as TeacherModel;
use think\Validate;
Class AdminTeacher extends Controller {
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
	//教师列表
	public function teacher_list(){
		$teacher=new TeacherModel();
		$name=input('get.realname');
		if (isset($name)){
		    $teacher=$teacher
                ->where('realname','like','%'.$name.'%')
                ->order('id')
                ->paginate(10);
        }else{
            $teacher=$teacher->order('id')->paginate(10);
        }
		$this->assign('teachers',$teacher);
		return $this->fetch();
	}
	//教师添加
	public function teacher_add(){
		$teacher=new TeacherModel();
		if ($this->request->isAjax()) {
			$date=input('post.');
			$validate=new Validate();
			$rule=[
				'username|用户名'=>'require|unique:teacher',
				'realname|真实姓名'=>'require|chsAlpha|min:2',
				'tel|手机号码'     =>'require',
				'email|邮箱'   =>'require|email',
                'password|密码'=>'require',
                'password1|确认密码'=>'require|confirm:password',
			];
			$va=$validate->check($date,$rule);
			if ($va==true) {
				$teacher=$teacher->save([
					'username' =>$date['username'],
					'password' =>$date['password'],
					'realname' =>$date['realname'],
					'tel'      =>$date['tel'],
					'email'    =>$date['email'],
					'statu'    =>1,]
				);
				if ($teacher) {
					$msg=$this->msg(200,'添加教师成功');
				}else{
					$msg=$this->msg(400,'添加教师失败');
				}
			}else{
				$msg=$this->msg(400,$validate->getError());
			}
			return $msg;
		}
		return $this->fetch();
	}
    //教师更新
    public function teacher_update($id){
        $teacher=new TeacherModel();
        if ($this->request->isAjax()) {
            $date=input('post.');
            $validate=new Validate();
            $rule=[
                'username|用户名'=>'require',
                'realname|真实姓名'=>'require',
                'tel|手机号码'     =>'require',
                'email|邮箱'   =>'require|email',
            ];
            $va=$validate->check($date,$rule);
            if ($va==true) {
                $teacher=$teacher->save([
                        'username' =>$date['username'],
                        'realname' =>$date['realname'],
                        'tel'      =>$date['tel'],
                        'email'    =>$date['email'],
                        ],['id'=>$id]
                );
                if ($teacher) {
                    $msg=$this->msg(200,'更新教师成功');
                }else{
                    $msg=$this->msg(400,'更新教师失败');
                }
            }else{
                $msg=$this->msg(400,$validate->getError());
            }
            return $msg;
        }
        $teacher=$teacher->where('id',$id)->find();
        $this->assign('teacher',$teacher);
        return $this->fetch();
    }
    //教师删除
    public function teacher_dele(){
	    $id=input('post.id');
	    $teacher=TeacherModel::destroy($id);
	    if ($teacher){
	        $msg=$this->msg(200,'删除成功');
        }else{
            $msg=$this->msg(400,'删除失败');
        }
	    return $msg;
    }
    //信息
	public function msg($statu,$msg){
		return [
			'statu' =>$statu,
			'msg'   =>$msg
		];
	}


}